package com.hrms.controller.admin;

import com.hrms.entity.Room;
import com.hrms.http.ResponseResult;
import com.hrms.service.RoomService;
import com.hrms.utils.EntityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Transactional
@RestController
@RequestMapping("/admin/roommanage/newroom")
public class AdminNewRoomController {
    // 管理员添加客房

    @Autowired
    private RoomService roomService;

    @RequestMapping("/add")
    public ResponseResult<Object> add(@RequestBody EntityUtil entity) {
        // 添加客房

        Integer room_id_start = entity.getRoom_id_start();
        Integer room_id_end = entity.getRoom_id_end();

        for(int room_id = room_id_start; room_id <= room_id_end; room_id++) {
            Room room = new Room();
            room.setRoom_id(String.valueOf(room_id));
            room.setRoom_price(entity.getRoom_price());
            room.setRoom_type(entity.getRoom_type());
            room.setRoom_status("空闲");

            // 房间号已存在
            if(roomService.idExist(room)) {
                // 回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return new ResponseResult<>("ERROR_room_id", null);
            }

            if(!roomService.add(room)) {
                // 回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return new ResponseResult<>("ERROR", null);
            }
        }

        return new ResponseResult<>("SUCCESS", null);
    }
}
